#include "CairoGraphics.h"

CairoGraphics::CairoGraphics()
{
	//Khoi tao giao dien
	HWND console = GetConsoleWindow();
	HDC dc = GetDC(console);

	cairo_surface_t *surface = cairo_win32_surface_create(dc);
	context = cairo_create(surface);
}
void CairoGraphics::drawLine(int x1, int y1, int x2, int y2, string stroke, int strokeWidth)
{
	string _stroke = stroke;
	//Parse chuoi stroke de ve color
	double red = Parse(stroke);
	double green = Parse(stroke);
	double blue = Parse(stroke);
	cairo_set_source_rgb(context,red,green,blue);
	//Thiet lap do rong
	cairo_set_line_width(context,strokeWidth);
	//Ve duong thang
	cairo_move_to(context, x1, y1);
	cairo_line_to(context, x2, y2);
	cairo_stroke(context);

	//Luu line nay vao trong chuoi listObjects de co the Save tap tin
	stringstream list;
	list << "<line x1=" << "\"" << x1 << "\"" << " y1=" << "\"" << y1 << "\"";
	list << " x2=" << "\"" << x2 << "\"" << " y2=" << "\"" << y2 << "\"";
	list << " stroke=" << "\"" << _stroke << "\""; 
	list << " stroke-width=" << "\"" << strokeWidth << "\"" << "/>";
	getListObjects() += list.str();
}
void CairoGraphics::drawRectangle(int x, int y, int width, int height, string stroke, int strokeWidth, string fill)
{
	string _stroke = stroke;
	string _fill = fill;

	//Xu ly chuoi string stroke tu rbg(,,) sang a1, a2, a3
	double red = Parse(stroke);
	double green = Parse(stroke);
	double blue = Parse(stroke);

	//Xu ly chuoi string fill tu rgb(,,) sang b1, b2, b3
	int b1 = Parse(fill);
	int b2 = Parse(fill);
	int b3 = Parse(fill);	

	//Thiet lap mau, do rong, ve hinh chu nhat
	cairo_set_source_rgb(context,red,green,blue);
	cairo_set_line_width(context,strokeWidth);
	cairo_rectangle(context, x, y, width, height);
	cairo_stroke(context);

	//Luu hinh chu nhat nay vao trong chuoi listObjects de co the Save tap tin
	stringstream list;
	list << "<rect x=" << "\"" << x << "\"" << " y=" << "\"" << y << "\"";
	list << " width=" << "\"" << width << "\"" << " height=" << "\"" << height << "\"";
	list << " stroke=" << "\"" << _stroke << "\""; 
	list << " stroke-width=" << "\"" << strokeWidth << "\"";
	list << " fill=" << "\"" << _fill << "\"" << "/>";
	getListObjects() += list.str();
}
void CairoGraphics::drawEllipse(int cx, int cy, int rx, int ry, string stroke, int strokeWidth, string fill)
{
	string _stroke = stroke;
	string _fill = fill;

	//Xu ly chuoi string stroke tu rbg(,,) sang a1, a2, a3
	double red = Parse(stroke);
	double green = Parse(stroke);
	double blue = Parse(stroke);

	//Xu ly chuoi string fill tu rgb(,,) sang b1, b2, b3
	int b1 = Parse(fill);
	int b2 = Parse(fill);
	int b3 = Parse(fill);	
	
	//Thiet lap mau, do rong, ve hinh ellipse
	cairo_set_source_rgb(context,red,green,blue);
	cairo_set_line_width(context,strokeWidth);
	cairo_scale(context, 1, 0.4);
	cairo_arc(context, cx, cy, 100, 0, 2*PI);
	cairo_stroke(context);

	//Luu vao chuoi listObjects de co the Save tap tin
	stringstream list;
	list << "<ellipse cx=" << "\"" << cx << "\"" << " cy=" << "\"" << cy << "\"";
	list << " rx=" << "\"" << rx << "\"" << " ry=" << "\"" << ry << "\"";
	list << " stroke=" << "\"" << _stroke << "\""; 
	list << " stroke-width=" << "\"" << strokeWidth << "\"";
	list << " fill=" << "\"" << _fill << "\"" << "/>";
	getListObjects() += list.str();
}